package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.*;
import com.xjc.business.entity.po.classadmin.ClassAdminStayBedApplyOpera;
import com.xjc.business.entity.po.stu.StayBedApplyPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.classadmin.ClassLeaveApplyVo;
import com.xjc.business.entity.vo.classadmin.ClassStayApplyVo;
import com.xjc.business.entity.vo.stu.HistoryStayBedApplyVo;
import com.xjc.business.entity.vo.stu.StayBedApplyVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.StudentStayBedApplysMapper;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【student_stay_bed_applys(学生留宿申请表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:32
*/
@Service
public class StudentStayBedApplysServiceImpl extends ServiceImpl<StudentStayBedApplysMapper, StudentStayBedApplys>
    implements StudentStayBedApplysService {

    @Autowired
    @Lazy
    WxUserService wxUserService;


    @Autowired
    @Lazy
    AllStudentRegsiterRelationsService allStudentRegsiterRelationsService;

    @Autowired
    @Lazy
    ClassAdminRelationsService classAdminRelationsService;

    @Autowired
    StudentsService studentsService;

    @Autowired
    @Lazy
    ClazzsService clazzsService;

    @Autowired
    @Lazy
    MajorsService majorsService;


    @Autowired
    @Lazy
    CollageAdminRelationsService collageAdminRelationsService;

    /**
     * 获取当前学生的留宿申请
     * @param id 学生微信id
     * @return Result
     */
    @Override
    public Result<StayBedApplyVo> getCurrentStayBedApply(Integer id) {
        Integer stuId = wxUserService.getById(id).getStuId();

        LambdaQueryWrapper<StudentStayBedApplys> studentStayBedApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentStayBedApplysLambdaQueryWrapper.eq(StudentStayBedApplys::getApplyId,stuId);
        studentStayBedApplysLambdaQueryWrapper.in(StudentStayBedApplys::getStatus,0,2);
        StudentStayBedApplys studentStayBedApplys = getOne(studentStayBedApplysLambdaQueryWrapper);

        if(studentStayBedApplys==null){
            return Result.okWithData(null);
        }

        StayBedApplyVo stayBedApplyVo = new StayBedApplyVo();
        stayBedApplyVo.setId(studentStayBedApplys.getId());
        stayBedApplyVo.setBatch(studentStayBedApplys.getBatch());
        stayBedApplyVo.setReason(studentStayBedApplys.getReason());
        stayBedApplyVo.setClassAdminStatus(studentStayBedApplys.getClassAdminStatus());
        stayBedApplyVo.setCollegeAdminStatus(studentStayBedApplys.getCollegeAdminStatus());
        stayBedApplyVo.setStatus(studentStayBedApplys.getStatus());
        stayBedApplyVo.setClassAdminInfo(studentStayBedApplys.getClassAdminInfo());
        stayBedApplyVo.setCollegeAdminInfo(studentStayBedApplys.getCollegeAdminInfo());
        stayBedApplyVo.setPhone(studentStayBedApplys.getPhone());
        stayBedApplyVo.setAccompanyingPerson(studentStayBedApplys.getAccompanyingPerson());
        stayBedApplyVo.setAccompanyingPersonPhone(studentStayBedApplys.getAccompanyingPersonPhone());
        stayBedApplyVo.setCreateTime(studentStayBedApplys.getCreateTime());

        return Result.okWithData(stayBedApplyVo);
    }

    /**
     * 获取当前学生的历史留宿申请
     * @param id 学生微信id
     * @return Result
     */
    @Override
    public Result<List<HistoryStayBedApplyVo>> getHistoryStayBedApplyList(Integer id) {
        Integer stuId = wxUserService.getById(id).getStuId();

        LambdaQueryWrapper<StudentStayBedApplys> studentStayBedApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentStayBedApplysLambdaQueryWrapper.eq(StudentStayBedApplys::getApplyId,stuId);
        studentStayBedApplysLambdaQueryWrapper.in(StudentStayBedApplys::getStatus,1);

        List<HistoryStayBedApplyVo> historyStayBedApplyVoList = list(studentStayBedApplysLambdaQueryWrapper).stream().map(studentStayBedApplys -> {
            HistoryStayBedApplyVo historyStayBedApplyVo = new HistoryStayBedApplyVo();

            historyStayBedApplyVo.setId(studentStayBedApplys.getId());
            historyStayBedApplyVo.setBatch(studentStayBedApplys.getBatch());
            historyStayBedApplyVo.setReason(studentStayBedApplys.getReason());
            historyStayBedApplyVo.setCreateTime(studentStayBedApplys.getCreateTime());

            return historyStayBedApplyVo;
        }).collect(Collectors.toList());

        return Result.okWithData(historyStayBedApplyVoList);
    }

    /**
     * 提交留宿申请
     * @param stayBedApplyPo 留宿申请信息
     * @return 反馈
     */
    @Override
    public Result<String> addStayBedApply(StayBedApplyPo stayBedApplyPo) {
        Integer stuId = wxUserService.getById(stayBedApplyPo.getApplyId()).getStuId();

        StudentStayBedApplys studentStayBedApplys = new StudentStayBedApplys();
        studentStayBedApplys.setApplyId(stuId);
        studentStayBedApplys.setBatch(stayBedApplyPo.getBatch());
        studentStayBedApplys.setReason(stayBedApplyPo.getReason());

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId,stuId);
        AllStudentRegsiterRelations studentFromDB = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper);
        Integer clazzId = studentFromDB.getClazzId();

        LambdaQueryWrapper<ClassAdminRelations> classAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classAdminRelationsLambdaQueryWrapper.eq(ClassAdminRelations::getClId,clazzId);
        ClassAdminRelations classAdminRelations = classAdminRelationsService.getOne(classAdminRelationsLambdaQueryWrapper);
        Integer clazzAdminId = classAdminRelations.getClazzAdminId();

        studentStayBedApplys.setClassAdminAuditorId(clazzAdminId);
        studentStayBedApplys.setClassAdminStatus(2);
        studentStayBedApplys.setPhone(stayBedApplyPo.getPhone());
        studentStayBedApplys.setAccompanyingPerson(stayBedApplyPo.getAccompanyingPerson());
        studentStayBedApplys.setAccompanyingPersonPhone(stayBedApplyPo.getAccompanyingPersonPhone());
        studentStayBedApplys.setStatus(2);
        studentStayBedApplys.setCreateTime(new Date());
        studentStayBedApplys.setUpdateTime(new Date());

        save(studentStayBedApplys);

        return Result.okWithData("提交成功");
    }

    /**
     * 获取学生留宿申请列表
     * @param pageSize     每页显示条数
     * @param currentPage  当前页
     * @param classAdminId 辅导员id
     * @param status       状态
     * @param stuName      学生姓名
     * @return Result
     */
    @Override
    public Result<PageVo<ClassStayApplyVo>> getStayApplyPage(Integer pageSize, Integer currentPage, Integer classAdminId, Integer status, String stuName) {

        LambdaQueryWrapper<ClassAdminRelations> classAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        classAdminRelationsLambdaQueryWrapper.eq(ClassAdminRelations::getClazzAdminId,classAdminId);
        List<Integer> classIdList = classAdminRelationsService.list(classAdminRelationsLambdaQueryWrapper).stream().map(ClassAdminRelations::getClId).collect(Collectors.toList());

        if(classIdList.isEmpty()){
            return Result.okWithData(new PageVo<>());
        }

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.in(AllStudentRegsiterRelations::getClazzId,classIdList);
        List<Integer> stuIdList = allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper).stream().map(AllStudentRegsiterRelations::getStuId).collect(Collectors.toList());

        LambdaQueryWrapper<Students> studentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentsLambdaQueryWrapper.like(Students::getName, stuName);
        List<Integer> collect = studentsService.list(studentsLambdaQueryWrapper).stream().map(Students::getId).collect(Collectors.toList());
        stuIdList.retainAll(collect);

        if(stuIdList.isEmpty()){
            return Result.okWithData(new PageVo<>());
        }
        LambdaQueryWrapper<StudentStayBedApplys> studentStayBedApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentStayBedApplysLambdaQueryWrapper.in(StudentStayBedApplys::getApplyId,stuIdList);
        studentStayBedApplysLambdaQueryWrapper.eq(status!=-1,StudentStayBedApplys::getClassAdminStatus,status);

        Page<StudentStayBedApplys> studentStayBedApplysPage = new Page<>(currentPage, pageSize);
        page(studentStayBedApplysPage,studentStayBedApplysLambdaQueryWrapper);
        List<StudentStayBedApplys> studentStayBedApplysPageRecords = studentStayBedApplysPage.getRecords();

        List<ClassStayApplyVo> classLeaveApplyVoList = studentStayBedApplysPageRecords.stream().map(studentStayBedApplys -> {
            ClassStayApplyVo classStayApplyVo = new ClassStayApplyVo();
            classStayApplyVo.setId(studentStayBedApplys.getId());
            classStayApplyVo.setStuName(studentsService.getById(studentStayBedApplys.getApplyId()).getName());

            Integer stuId = studentStayBedApplys.getApplyId();
            LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            allStudentRegsiterRelationsLambdaQueryWrapper2.eq(AllStudentRegsiterRelations::getStuId,stuId);
            AllStudentRegsiterRelations studentFromDB = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper2);
            Integer classId = studentFromDB.getClazzId();
            Integer majId = studentFromDB.getMajId();

            classStayApplyVo.setClassName(majorsService.getById(majId).getName()+clazzsService.getById(classId).getNum()+"班");
            classStayApplyVo.setBatch(studentStayBedApplys.getBatch());
            classStayApplyVo.setReason(studentStayBedApplys.getReason());

            classStayApplyVo.setPhone(studentStayBedApplys.getPhone());
            classStayApplyVo.setAccompanyingPerson(studentStayBedApplys.getAccompanyingPerson());
            classStayApplyVo.setAccompanyingPersonPhone(studentStayBedApplys.getAccompanyingPersonPhone());

            classStayApplyVo.setClassAdminStatus(studentStayBedApplys.getClassAdminStatus());
            classStayApplyVo.setCollegeAdminStatus(studentStayBedApplys.getCollegeAdminStatus());
            classStayApplyVo.setStatus(studentStayBedApplys.getStatus());
            classStayApplyVo.setClassAdminInfo(studentStayBedApplys.getClassAdminInfo());
            classStayApplyVo.setCollegeAdminInfo(studentStayBedApplys.getCollegeAdminInfo());
            classStayApplyVo.setCreateTime(studentStayBedApplys.getCreateTime());
            return classStayApplyVo;
        }).collect(Collectors.toList());

        PageVo<ClassStayApplyVo> pageVo = new PageVo<>();
        pageVo.setData(classLeaveApplyVoList);
        pageVo.setTotal(studentStayBedApplysPage.getTotal());
        pageVo.setPageCount(studentStayBedApplysPage.getPages());
        pageVo.setCurrent(currentPage);
        pageVo.setSize(pageSize);

        return Result.okWithData(pageVo);
    }

    /**
     * 获取学生留宿申请列表
     * @param pageSize     每页显示条数
     * @param currentPage  当前页
     * @param collegeAdminId 辅导员id
     * @param status       状态
     * @param stuName      学生姓名
     * @return Result
     */
    @Override
    public Result<PageVo<ClassStayApplyVo>> getStayApplyPage2(Integer pageSize, Integer currentPage, Integer collegeAdminId, Integer status, String stuName) {

        LambdaQueryWrapper<CollageAdminRelations> collageAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageAdminRelationsLambdaQueryWrapper.eq(CollageAdminRelations::getColAdminId,collegeAdminId);
        Integer colId = collageAdminRelationsService.getOne(collageAdminRelationsLambdaQueryWrapper).getColId();

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getColId,colId);
        List<Integer> stuIdList = allStudentRegsiterRelationsService.list(allStudentRegsiterRelationsLambdaQueryWrapper).stream().map(AllStudentRegsiterRelations::getStuId).collect(Collectors.toList());

        LambdaQueryWrapper<Students> studentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentsLambdaQueryWrapper.like(Students::getName,stuName);
        List<Integer> collectIdList = studentsService.list(studentsLambdaQueryWrapper).stream().map(Students::getId).collect(Collectors.toList());

        stuIdList.retainAll(collectIdList);

        if(stuIdList.isEmpty()){
            return Result.okWithData(new PageVo<>());
        }


        LambdaQueryWrapper<StudentStayBedApplys> studentStayBedApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentStayBedApplysLambdaQueryWrapper.in(StudentStayBedApplys::getApplyId,stuIdList);
        studentStayBedApplysLambdaQueryWrapper.eq(status!=-1,StudentStayBedApplys::getCollegeAdminStatus,status);
        studentStayBedApplysLambdaQueryWrapper.eq(StudentStayBedApplys::getClassAdminStatus,1);

        Page<StudentStayBedApplys> studentStayBedApplysPage = new Page<>(currentPage, pageSize);
        page(studentStayBedApplysPage,studentStayBedApplysLambdaQueryWrapper);
        List<StudentStayBedApplys> studentStayBedApplysPageRecords = studentStayBedApplysPage.getRecords();

        List<ClassStayApplyVo> classStayApplyVoList = studentStayBedApplysPageRecords.stream().map(studentStayBedApplys -> {
            ClassStayApplyVo classStayApplyVo = new ClassStayApplyVo();
            classStayApplyVo.setId(studentStayBedApplys.getId());
            classStayApplyVo.setStuName(studentsService.getById(studentStayBedApplys.getApplyId()).getName());
            Integer stuId = studentStayBedApplys.getApplyId();
            LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            allStudentRegsiterRelationsLambdaQueryWrapper2.eq(AllStudentRegsiterRelations::getStuId,stuId);
            AllStudentRegsiterRelations studentFromDB = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper2);
            Integer classId = studentFromDB.getClazzId();
            Integer majId = studentFromDB.getMajId();
            classStayApplyVo.setClassName(majorsService.getById(majId).getName()+clazzsService.getById(classId).getNum()+"班");
            classStayApplyVo.setBatch(studentStayBedApplys.getBatch());
            classStayApplyVo.setReason(studentStayBedApplys.getReason());
            classStayApplyVo.setPhone(studentStayBedApplys.getPhone());
            classStayApplyVo.setAccompanyingPerson(studentStayBedApplys.getAccompanyingPerson());
            classStayApplyVo.setAccompanyingPersonPhone(studentStayBedApplys.getAccompanyingPersonPhone());

            classStayApplyVo.setClassAdminStatus(studentStayBedApplys.getClassAdminStatus());
            classStayApplyVo.setCollegeAdminStatus(studentStayBedApplys.getCollegeAdminStatus());
            classStayApplyVo.setStatus(studentStayBedApplys.getStatus());
            classStayApplyVo.setClassAdminInfo(studentStayBedApplys.getClassAdminInfo());
            classStayApplyVo.setCollegeAdminInfo(studentStayBedApplys.getCollegeAdminInfo());
            classStayApplyVo.setCreateTime(studentStayBedApplys.getCreateTime());
            return classStayApplyVo;
        }).collect(Collectors.toList());
        PageVo<ClassStayApplyVo> pageVo = new PageVo<>();
        pageVo.setData(classStayApplyVoList);
        pageVo.setTotal(studentStayBedApplysPage.getTotal());
        pageVo.setPageCount(studentStayBedApplysPage.getPages());
        pageVo.setCurrent(currentPage);
        pageVo.setSize(pageSize);

        return Result.okWithData(pageVo);
    }

    /**
     * 辅导员审批留宿申请
     * @param classAdminStayBedApplyOpera 审批意见
     * @return Result
     */
    @Override
    public Result<String> leaveApplyOpera(ClassAdminStayBedApplyOpera classAdminStayBedApplyOpera) {

        Integer id = classAdminStayBedApplyOpera.getId();
        StudentStayBedApplys studentStayBedApplys = getById(id);
        studentStayBedApplys.setClassAdminStatus(classAdminStayBedApplyOpera.getClassAdminStatus());
        studentStayBedApplys.setClassAdminInfo(classAdminStayBedApplyOpera.getClassAdminInfo());

        updateById(studentStayBedApplys);
        return Result.okWithData("审批完成");
    }

    /**
     * 学院管理员审批留宿申请
     * @param classAdminStayBedApplyOpera 审批意见
     * @return Result
     */
    @Override
    public Result<String> leaveApplyOpera2(ClassAdminStayBedApplyOpera classAdminStayBedApplyOpera) {
        Integer id = classAdminStayBedApplyOpera.getId();
        StudentStayBedApplys studentStayBedApplys = getById(id);
        studentStayBedApplys.setCollegeAdminStatus(classAdminStayBedApplyOpera.getClassAdminStatus());
        studentStayBedApplys.setCollegeAdminInfo(classAdminStayBedApplyOpera.getClassAdminInfo());
        studentStayBedApplys.setStatus(classAdminStayBedApplyOpera.getClassAdminStatus());

        updateById(studentStayBedApplys);
        return Result.okWithData("审批完成");
    }

}




